<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-10-13
 * Time: 01:48
 */

namespace app\services;


use app\models\GdzyzSchool;

class GdzyzSchoolService extends ServiceBase
{
    /**
     * @param array $params
     * @param int $page
     * @param int $pagesize
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($params=[], $page=1, $pagesize=10)
    {
        return GdzyzSchool::lists($params, $page, $pagesize);
    }

    /**
     * @param $id
     * @return GdzyzSchool|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetail($id)
    {
        $school = GdzyzSchool::find($id);

        if ($school instanceof GdzyzSchool) {
            return $school;
        }

        throw new \Exception('学校不存在');
    }

    /**
     * 添加组织
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function create($params)
    {
        try {
            $school = new GdzyzSchool();

            foreach ($params as $key => $val) {
                $school->$key = $val;
            }
            $school->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param GdzyzSchool $school
     * @param $params
     */
    public function update(GdzyzSchool $school, $params)
    {
        foreach ($params as $key => $val) {
            $school->$key = $val;
        }
        $school->save();
    }

    public function delete(GdzyzSchool $school)
    {
        $school->delete();
    }
}